<template>
  <view class="switchAccount_container">
    <view class="itemCard" v-for="(item, index) in list" :key="index" @click="clickItem(item)">
      <view v-if="item.active" class="currentAccount"> 当前账号 </view>
      <view class="card-1">
        <image :src="staticUrl() + '/setting/img.png'"></image>
        <text>{{ item.name }}</text>
      </view>
      <view class="card-2">
        <text>收货人：</text>
        <text>{{ item.consigneeName || '' }}</text>
      </view>
      <view class="card-3">
        手机号：<text>{{ item.consigneeMobile }}</text>
      </view>
      <view class="card-3">
        用户名：<text>{{ item.username }}</text>
        <text @click.stop="copy(item)">复制</text>
      </view>
    </view>

    <view style="height: 50rpx"></view>
  </view>
</template>

<script>
import { getRelevances, switchoverLogin } from '@/api/login/wechat.js'
import jwtDecode from 'jwt-decode'

export default {
  data () {
    return {
      list: [],
      purchaserId: ''
    }
  },
  onLoad () {
    let a = JSON.parse(jwtDecode(uni.getStorageSync('token')).userContext.replace('\\', ''))
    this.purchaserId = a.purchaserId
    getRelevances({
      id: this.purchaserId
    }).then(res => {
      if (res.code === 200) {
        this.list = res.data
      }
    })
  },
  methods: {
    copy (item) {
      uni.setClipboardData({
        data: item.username,
        success: function () {
          uni.showToast({ icon: 'success', title: '复制成功', mask: true })
        },
        fail: function () {
          uni.showToast({ icon: 'error', title: '复制失败', mask: true })
        }
      })
    },
    clickItem (val) {
      if (val.id === this.purchaserId) {
        uni.showToast({ icon: 'error', title: '已是此账号', mask: true })
      } else {
        switchoverLogin({
          username: val.username
        }).then(res => {
          if (res.code === 200) {
            uni.setStorageSync('token', res.data.accessToken)
            this.goHome()
            uni.showToast({ icon: 'none', title: '切换成功', mask: true })
          }
        })
      }
    },
    goHome () {
      uni.switchTab({
        url: '/pages/index/index',
        success: res => {
          // let page = getCurrentPages().pop();
          // if (page == undefined || page == null) return;
          // page.onLoad()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.switchAccount_container {
  width: 100%;
  min-height: 100vh;
  background: #fff;

  .itemCard {
    position: relative;
    background: url("https://www.xdyy100.com/wholesale/files/mp/static/setting/accountNumber.png")
      no-repeat;
    background-size: 100% 100%;
    // height: 343.75rpx;
    margin: 20.83rpx 25rpx;
    padding: 56rpx 53rpx;
    font-size: 28rpx;
    color: #204e77;

    .currentAccount {
      position: absolute;
      top: 38rpx;
      right: 0;
      width: 156rpx;
      height: 48rpx;
      line-height: 48rpx;
      text-align: center;
      font-size: 22rpx;
      background: #ffffff;
      border-radius: 22rpx 0rpx 0rpx 22rpx;
    }

    .card-1 {
      height: 76rpx;
      display: flex;
      flex-flow: row nowrap;
      align-items: center;
      border-bottom: 2rpx dashed #6497c4;
      padding-bottom: 21.53rpx;

      image {
        width: 58.33rpx;
        height: 58.33rpx;
        margin-right: 20.83rpx;
      }

      text {
        width: 70%;
      }
    }

    .card-2 {
      margin-top: 54.17rpx;
    }

    .card-3 {
      margin-top: 22.22rpx;

      text:nth-child(2) {
        background: #5c99c4;
        border-radius: 40rpx;
        padding: 4rpx 14rpx;
        margin-left: 50rpx;
        font-size: 28rpx;
        color: #fff;
      }
    }
  }
}
</style>
